package com.tebiecloud.store.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.store.client.model.entity.StoreStaff;
import com.tebiecloud.store.client.service.IStoreStaffServiceClient;
import com.tebiecloud.store.server.service.StoreStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/feign/staff")
public class StaffController implements IStoreStaffServiceClient {

    @Autowired
    StoreStaffService storeStaffService;

    @PostMapping("selectStoreStaff")
    @Override
    public ResultBody<StoreStaff> selectStoreStaff(@RequestParam("storeId") long storeId,
                                                   @RequestParam(value = "userId",required = false) Long userId,
                                                   @RequestParam(value = "roleCode",required = false) String roleCode) {
        QueryWrapper<StoreStaff> storeStaffQueryWrapper = new QueryWrapper<>();
        storeStaffQueryWrapper.eq("store_id",storeId);
        if(userId!=null)storeStaffQueryWrapper.eq("user_id",userId);
        if(StringUtils.isNotEmpty(roleCode))storeStaffQueryWrapper.eq("role_code",roleCode);
        storeStaffQueryWrapper.orderByDesc("ct");
        storeStaffQueryWrapper.last("LIMIT 1");
        StoreStaff storeStaff = storeStaffService.getOne(storeStaffQueryWrapper);
        if(storeStaff==null) return ResultBodyUtils.failed();
        return ResultBodyUtils.ok(storeStaff);
    }

    @PostMapping("updateStoreStaff")
    @Override
    public ResultBody<StoreStaff> updateStoreStaff(@RequestParam("userId") long userId,
                                                   @RequestParam("nickName") String nickName,
                                                   @RequestParam("headimgurl") String headimgurl) {
        UpdateWrapper<StoreStaff> storeStaffUpdateWrapper = new UpdateWrapper<>();
        storeStaffUpdateWrapper.eq("user_id",userId);
        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setName(nickName);
        storeStaff.setHeadimgurl(headimgurl);
        storeStaffService.update(storeStaff,storeStaffUpdateWrapper);
        return ResultBodyUtils.ok(storeStaff);
    }
}
